package org.jks.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.GenericGenerator;
import org.jks.report.model.ReportDROrderModel;
import org.jks.report.model.ReportDeviceRecipientModel;
import org.jks.util.DateUtil;

import com.google.gson.annotations.Expose;

@Entity
@Table(name = "DeviceRecipient")
public class DeviceRecipient {
	@Id
	@GeneratedValue(generator = "increment")
	@GenericGenerator(name = "increment", strategy = "increment")
	@Expose
	private long Id;
	@Column(name = "OrderNo", length = 64, nullable = false)
	@Expose
	private String OrderNo;
	@ManyToOne
	@JoinColumn(name = "Department", referencedColumnName = "Id", nullable = false)
	@Expose
	private Department Department;
	@ManyToOne
	@JoinColumn(name = "[Group]", referencedColumnName = "Id", nullable = false)
	@Expose
	private Flight Group;
	@ManyToOne
	@JoinColumn(name = "Recipients", referencedColumnName = "Id", nullable = false)
	@Expose
	private User Recipients;
	@Temporal(TemporalType.TIMESTAMP)
	@Expose
	private Date Time;
	@Lob
	@Expose
	private String Desp;
	@OneToMany(mappedBy = "DeviceRecipient", fetch = FetchType.LAZY, cascade = { CascadeType.ALL }, orphanRemoval = true)
	@Expose
	private List<RecipientOrder> RecipientOrders;
	


	public long getId() {
		return Id;
	}

	public void setId(long id) {
		Id = id;
	}

	public String getOrderNo() {
		return OrderNo;
	}

	public void setOrderNo(String orderNo) {
		OrderNo = orderNo;
	}

	public Department getDepartment() {
		return Department;
	}

	public void setDepartment(Department department) {
		Department = department;
	}

	public Flight getGroup() {
		return Group;
	}

	public void setGroup(Flight group) {
		Group = group;
	}

	public User getRecipients() {
		return Recipients;
	}

	public void setRecipients(User recipients) {
		Recipients = recipients;
	}

	public Date getTime() {
		return Time;
	}

	public void setTime(Date time) {
		Time = time;
	}

	public String getDesp() {
		return Desp;
	}

	public void setDesp(String desp) {
		Desp = desp;
	}

	public List<RecipientOrder> getRecipientOrders() {
		return RecipientOrders;
	}

	public void setRecipientOrders(List<RecipientOrder> recipientOrders) {
		RecipientOrders = recipientOrders;
	}


	
	public ReportDeviceRecipientModel toReportModel(){
		ReportDeviceRecipientModel rdrm = new ReportDeviceRecipientModel();
		rdrm.setId(this.Id + "");
		rdrm.setOrderNo(this.OrderNo);
		rdrm.setDepartment(this.Department.getName());
		rdrm.setFlight(this.Group.getName());
		rdrm.setDesp(this.Desp);
		rdrm.setRecipients(this.Recipients.getName());
		rdrm.setRecipientTime(DateUtil.toString(this.getTime(), "yyyy-MM-dd HH:mm:ss"));
		
		if(this.RecipientOrders != null && this.RecipientOrders.size() > 0){
			List<ReportDROrderModel> orders = new ArrayList<ReportDROrderModel>();
			for(RecipientOrder order : this.RecipientOrders){
				ReportDROrderModel rdrom = new ReportDROrderModel();
				rdrom.setId(order.getId() + "");
				rdrom.setDeviceName(order.getDevice().getType().getName());
				rdrom.setDeviceModel(order.getDevice().getModel());
				rdrom.setCount(order.getCount() + "");
				orders.add(rdrom);
			}
			rdrm.setOrders(orders);
		}
		return rdrm;
	}
}
